package com.yxq.mdt.app.modules.socialcontact.ui.fragment.main;


import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.socialcontact.adapter.NearbyAllListAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.NearManAndDynamicTopicBean;
import com.yxq.mdt.app.modules.socialcontact.bean.PerfectBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SelectNearbyListBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialNearContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialNearPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * @author L
 * @time
 * @describe 附近的人列表 nv
 */
public class NearbyWoListFragment extends BaseFragment<MainSocialNearPresenter> implements MainSocialNearContract.View {
    @BindView(R.id.nearby_recycler)
    RecyclerView nearbyRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private NearbyAllListAdapter nearbyDetailsAdapter;
    String isMenAndWomen;
    private List<SelectNearbyListBean.ListBean> listBeans;
    double mapLng, mapLat;


    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_nearbey_list_detalils;
    }

    @Override
    protected MainSocialNearPresenter getPresenter() {
        return new MainSocialNearPresenter(this, new MainSocialContactModel());
    }


    @Override
    public void initView(View view) {
        listBeans = new ArrayList<>();
        Bundle bundle = getArguments();
        if (bundle != null) {
            isMenAndWomen = bundle.getString("isMenAndWomen");
        }
        if (Hawk.contains(SPKey.SP_KEY_USER_ADDRESS_MAP_LNG)) {
            mapLng = Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LNG);
            mapLat = Hawk.get(SPKey.SP_KEY_USER_ADDRESS_MAP_LAT);
        }
        if (mapLng == 0 || mapLat == 0) {
            mapLng = 106.63043;
            mapLat = 29.71798;
        }
        nearbyRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        nearbyDetailsAdapter = new NearbyAllListAdapter(R.layout.item_nearby_details_list, listBeans);
        nearbyRecycler.setAdapter(nearbyDetailsAdapter);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getSelectMyNearby(page, 30, String.valueOf(mapLng), String.valueOf(mapLat), "2");
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getSelectMyNearby(page, 30, String.valueOf(mapLng), String.valueOf(mapLat), "2");
        });
        nearbyDetailsAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
            it.putExtra("userId", String.valueOf(nearbyDetailsAdapter.getItem(position).getUserId()));
            startActivity(it);

        });
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onLoadNearManAndDynamicSuccess(String s) {

    }

    @Override
    public void onLoadNearManAndDynamicFailed() {
    }

    @Override
    public void onLoadSelectMyNearbySuccess(SelectNearbyListBean selectNearbyListBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (page == 1) {
            nearbyDetailsAdapter.setNewData(selectNearbyListBean.getList());

        } else {
            if (selectNearbyListBean.getList() != null && selectNearbyListBean.getList().size() != 0) {
                nearbyDetailsAdapter.addData(selectNearbyListBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        nearbyDetailsAdapter.setOnItemChildClickListener((adapter, view1, position) ->

        {
            switch (view1.getId()) {
                case R.id.nearby_nike_img:
                    //个人主页
                    Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
                    it.putExtra("userId", String.valueOf(nearbyDetailsAdapter.getItem(position).getUserId()));
                    startActivity(it);
                    break;
                case R.id.nearby_attention_tv:
                    TextView attention = view1.findViewById(R.id.nearby_attention_tv);
                    MainSocialContactModel.setInterestRelation(nearbyDetailsAdapter.getItem(position).getUserId() + "", 1, 1, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            attention.setVisibility(View.GONE);
                            showCenterToast("关注成功");

                        }

                        @Override
                        public void onError(String errMsg) {
                            showCenterToast("关注失败");
                        }
                    });
                    break;
            }
        });

    }

    @Override
    public void onLoadSelectMyNearbyFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadNearManAndDynamicTopicSuccess(NearManAndDynamicTopicBean nearManAndDynamicTopicBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadNearManAndDynamicTopicFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadQueryIsPerfectFailed() {

    }

    @Override
    public void onLoadQueryIsPerfectSuccess(PerfectBean s) {

    }
}
